package vn.vfriends.vbox.rest;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import vn.vfriends.vbox.facade.UserFacade;
import vn.vfriends.vbox.interceptor.LoggingInterceptor;
import vn.vfriends.vbox.model.User;

/**
 *
 * @author tuancn
 */
@Interceptors(LoggingInterceptor.class)
@Stateless
@Path("users")
public class UserService {
    
    @Inject
    private UserFacade userFacade;

    @POST
    @Consumes({"application/xml", "application/json"})
    public User createUser(@NotNull User user) {
        userFacade.create(user);
        return user;
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public User editUser(@NotNull User user) {
        userFacade.edit(user);
        return user;
    }

    @DELETE
    @Path("{id: \\d+}")
    public void remove(@PathParam("id") Integer id) {
        userFacade.remove(userFacade.find(id));
    }

    @GET
    @Path("{id: \\d+}")
    @Produces({"application/xml", "application/json"})
    public User find(@PathParam("id") Integer id) {
        return userFacade.find(id);
    }

    @GET
    @Path("find")
    @Produces({"application/xml", "application/json"})
    public User find(@NotNull @QueryParam("username") String username, @NotNull @QueryParam("provider") String provider) {
        return userFacade.find(username, provider);
    }
    
    @GET
    @Produces({"application/xml", "application/json"})
    public List<User> findAll() {
        return userFacade.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<User> findRange(@NotNull @PathParam("from") Integer from, @NotNull @PathParam("to") Integer to) {
        return userFacade.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    public int count() {
        return userFacade.count();
    }
}
